//
//  PEParaConfigVC.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/10/28.
//

#import "RHParaSelectVC.h"
#import "RHDeviceParaCell.h"
#import "RHParaHeaderView.h"
#import "RHParaConfigModel.h"

@interface RHParaSelectVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectView;

@property (nonatomic, strong) NSArray *paraArr;
@end

@implementation RHParaSelectVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = CFTabViewBgColor;
    self.navigationItem.title = @"参数选择".rh_localized;
    [self setupUI];
    [self getAllEndpointData];
}

-(void)setupUI
{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    _collectView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectView.backgroundColor = [UIColor whiteColor];;
    _collectView.showsVerticalScrollIndicator = NO;
    _collectView.showsHorizontalScrollIndicator = NO;
    _collectView.delegate = self;
    _collectView.dataSource = self;
    [_collectView registerClass:[RHDeviceParaCell class] forCellWithReuseIdentifier:@"RHDeviceParaCell"];
    [_collectView registerClass:[RHParaHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.view addSubview:_collectView];
    
    
    [_collectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(15, 15, 15, 15));
    }];
    
    kWeakSelf;
    [_collectView addHeaderRefresh:^{
        [weakSelf getAllEndpointData];
    }];
    
    UIButton *detailBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,40, 64)];
    [detailBtn setTitle:@"确定".rh_localized  forState:UIControlStateNormal];
    [detailBtn setBackgroundColor:UIColor.clearColor];
    [detailBtn setTitleColor:[UIColor colorWithHexString:@"56C271" alpha:1] forState:UIControlStateNormal];
    [detailBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
//        [detailBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [detailBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:detailBtn];
    self.navigationItem.rightBarButtonItem = rightBarBtn;
}

-(void)sureAction
{
    if(self.selectParaBlock){
        self.selectParaBlock(self.paraArr);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)getAllEndpointData
{
    [QMUITips showLoadingInView:self.view];
    [RHParaConfigModel startGetWithCompletionAPIKey:H_getAllEndpointData argument:@{} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.collectView endHeaderRefresh];
        if(resultModel.code == 200){
            self.paraArr = resultModel.data;
            [self.collectView reloadData];
        }
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        [self.collectView endHeaderRefresh];
    }];
}


#pragma mark ------------ collectionView delegate -------
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.paraArr.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    RHParaConfigModel *sectionModel = self.paraArr[section];
    return sectionModel.keyCodes.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    RHParaConfigModel *sectionModel = self.paraArr[indexPath.section];
    if([kind isEqualToString:UICollectionElementKindSectionHeader]){
        RHParaHeaderView *headerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        headerview.titleLab.text = sectionModel.endpointTypeCode;
        return headerview;
    }
    return [UICollectionReusableView new];
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(kUI_WINDOW_W, 50);
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RHParaConfigModel *sectionModel = self.paraArr[indexPath.section];
    RHDeviceParaCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RHDeviceParaCell" forIndexPath:indexPath];
    cell.paraModel = sectionModel.keyCodes[indexPath.item];
    return cell;

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
  
    CGFloat w = (kUI_WINDOW_W -30-20 - 30)/3;
    CGFloat h = w * 0.45;
    return CGSizeMake(w, h);
 
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(15, 15, 15, 15);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
  
   return 10;
   
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    RHParaConfigModel *sectionModel = self.paraArr[indexPath.section];
    DeviceChartFieldModel *paraModel = sectionModel.keyCodes[indexPath.item];
    paraModel.isSelected = !paraModel.isSelected;
    [collectionView reloadData];
}

@end
